-- 闪现

local flash_distance = 3
local flashing = false
local behavior

return {
onBeforeMissionBehaviorInitialize = function (mission)
    behavior = SubModule:CreateMissionBehavior({
        onMissionTick = function (dt)
            if not flashing and Input.IsKeyPressed(InputKey.Q) then
                local player = Agent.Main
                if player == nil or player.Health <= 0 then
                    return
                end
                local pos = player.Position + player.Velocity * flash_distance
                if player:CanMoveDirectlyToPosition(pos.AsVec2) then
                    flashing = true
                    local visuals = player.AgentVisuals
                    player.Mission:AddParticleSystemBurstByName("dust_fall_a", MatrixFrame(Mat3.Identity, player.Position + Vec3.Up), true)
                    player.Mission:MakeSoundOnlyOnRelatedPeer(SoundEvent.GetEventIdFromString("event:/mission/combat/impact/ballista_missile/straw"), player.Position, player.Index)
                    player.Mission:AddSoundAlarmFactorToAgents(player.Index, player.Position, 15)
                    local lastMortalityState = player.CurrentMortalityState
                    player:SetMortalityState(Agent.MortalityState.Invulnerable)
                    behavior:SetTimeout(100, function (dt2)
                        visuals:SetVisible(false)
                        if player.MountAgent ~= nil then
                            player.MountAgent.AgentVisuals:SetVisible(false)
                        end
                    end)
                    behavior:SetTimeout(200, function (dt2)
                        visuals:SetVisible(true)
                        if player.MountAgent ~= nil then
                            player.MountAgent.AgentVisuals:SetVisible(true)
                        end
                        player:TeleportToPosition(pos)
                        player:SetMortalityState(lastMortalityState)
                        flashing = false
                    end)
                end
            end
        end
    })
    mission:AddMissionBehavior(behavior)
end
}